var express = require('express');
var router = express.Router();
var Goods = require('./../models/goods');

//查询商品列表
router.get('/', function(req, res, next) {
	let page = parseInt(req.param('page'));
	let pageSize = parseInt(req.param('pageSize'));
	let priceLevel = req.param('priceLevel');
    let sort = req.param('sort');
	let skip = (page-1)*pageSize;
	var priceGt = '',priceLt = '';
    let params = {};
	if(priceLevel!=='all'){
        switch(priceLevel){
            case '0': priceGt = 0;priceLt = 500; break;
            case '1': priceGt = 500;priceLt = 1000; break;
            case '2': priceGt = 1000;priceLt = 2000; break;
        }
        params = {
        	salePrice:{
        		$gt:priceGt,
				$lte:priceLt
			}
		}
	}
	let goodsModel = Goods.find(params).skip(skip).limit(pageSize);
	goodsModel.sort({"salePrice":sort});
	goodsModel.exec(function(err,doc){
		if(err){
			res.json({
				status:'1',
				msg:err.message,
                result:''
			})
		}else{
            res.json({
                status:'0',
                msg:'',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
		}
	})
});

//加入购物车
router.post('/addCart',function(req,res,next){
	let userId = req.cookies.userId;
	let productId = req.body.productId;
	var User = require('../models/user');

	//获取当前用户文档
	User.findOne({userId:userId},function(err,userDoc){
		if(err){
			res.json({
				status:'1',
				msg:err.message
			})
		}else{
			if(userDoc){
				let goodsItem = '';
				userDoc.cartList.forEach(function(item){
					if(item.productId == productId){
                        goodsItem = item;
                        item.productNum ++;
					}
				})
				//有值说明已有该商品，否则是新添加的商品
				if(goodsItem){
                    userDoc.save(function(err2,doc2){
                        if(err2){
                            res.json({
                                status: '1',
                                msg: '',
                                result: '加入失败'
                            })
                        }else {
                            res.json({
                                status: '0',
                                msg: '',
                                result: '加入成功'
                            })
                        }
                    })
				}else{
                    //获取当前用户购物车列表里的商品
                    Goods.findOne({productId:productId},function(err1,doc){
                        if(err1){
                            res.json({
                                status:'1',
                                msg:err1.message
                            })
                        }else{
                            if(doc){
                                doc.productNum = 1;
                                doc.checked = 1;
                                userDoc.cartList.push(doc);
                                userDoc.save(function(err2,doc2){
                                    if(err2){
                                        res.json({
                                            status: '1',
                                            msg: '',
                                            result: '加入失败'
                                        })
                                    }else {
                                        res.json({
                                            status: '0',
                                            msg: '',
                                            result: '加入成功'
                                        })
                                    }
                                })
                            }
                        }
                    })
				}
			}else{
                res.json({
                    status: '1',
                    msg: '请先登录',
                    result: ''
                })
            }
		}
	})
})

module.exports = router;